<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>基础语法6 v-for列表渲染</title>
    <script src="http://unpkg.com/vue@3"></script>
</head>
<body>
    <div id="app">
        <h1>列表渲染主要是通过v-for指令来实现的</h1>
        <h1>知识点1:迭代普通数据</h1>
            <ul>
                <li v-for="item in times">桃花</li>
            </ul>
            <ul>
                <li v-for="item in flowers">{{item}}</li>
            </ul>
        <h1>知识点2:迭代对象数</h1>
            <ul>
                <li v-for="(item,index) in users">{{item.id}}---{{index}}</li>
            </ul>
        <h1>知识点3:迭代对象里的属性</h1>
            <ul>
                <li v-for="(val,key) in users">{{key}}--{{val}}</li>
            </ul>
        <h1>知识点4:迭代数字</h1>
            <ul>
                <li v-for="item in 10">{{item}}</li>
            </ul>
        <h1>知识点5:关于key属性</h1>
            ID:   <input type="text" v-model="id">
            NAME: <input type="text" v-model="name"><br>
            <button @click="add">添加</button>
            <button @click="del">删除</button><br>
            我新添加的ID是{{id}},我新添加的NAME是{{name}}<br>
            <table border="1">
                <tr v-for="(item,index) in users" :key="index">
                    <td><input type="checkbox"></td>
                    <td>{{item.id}}</td>
                    <td>{{item.name}}</td>
                </tr>
            </table><br>
            <h3>为了提升循环性能,一般会给每个循环加上一个唯一的key值,在实际中,后端会向前端返回数据时,
                携带一个唯一的与数据相关的数据条目标识符id,通常时一个随机字段
            </h3>
        <h1>知识点6：v-for的嵌套</h1>
            <ul>
                <li v-for="(item,index) in users">用户{{index+1}}
                    <div>
                        <p v-for="(list,i) in item">{{i}}:{{list}}</p>
                    </div>
                </li>
            </ul>
    </div>
</body>
</html>

<script>
    const appConn = Vue.createApp({
        data() {
            return {
                times:5,
                flowers:['桃花','梅花','梨花','菊花','兰花','杏花'],
                users:[{id:'001',name:'高圆圆'},{id:'002',name:'王语嫣'}]
            }
        },
        methods: {
            add(){
                this.users.push({id:this.id,name:this.name})
                //push是从数组的尾部进行添加,unshift是从头部进行添加
            },
            del(){
                this.users.pop()
                //pop是从数组的尾部进行删除,shift是从头部进行删除
            }
        },
    }).mount("#app")
</script>